package com.x.reader.controller;

import com.x.framework.Base;
import com.x.framework.model.ResData;
import com.x.reader.model.Book;
import com.x.reader.model.History;
import com.x.reader.service.BookService;
import com.x.reader.service.HistoryService;
import com.x.reader.util.Constant;
import com.x.reader.util.PdfUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

@Controller
@RestController
public class BookController extends Base {

    @Autowired
    private BookService bookService;

    @Autowired
    private HistoryService historyService;

    @RequestMapping(value = "/SelectBookRecommendList")
    public ResData selectBookRecommendList(Book book) {
        List<Book> bookList = bookService.selectBookRecommendList(book);
        ResData resData = new ResData(1);
        resData.setResult(bookList);
        return resData;
    }

    @RequestMapping(value = "/SelectBookNewList")
    public ResData selectBookNewList(Book book) {
        List<Book> bookList = bookService.selectBookNewList(book);
        ResData resData = new ResData(1);
        resData.setResult(bookList);
        return resData;
    }

    @RequestMapping(value = "/SelectBookRankList")
    public ResData selectBookRankList(Book book) {
        List<Book> bookList = bookService.selectBookRankList(book);
        ResData resData = new ResData(1);
        resData.setResult(bookList);
        return resData;
    }

    @RequestMapping(value = "/SelectBookList")
    public ResData selectBookList(Book book) {
        List<Book> bookList = bookService.selectBookList(book);
        ResData resData = new ResData(1);
        resData.setResult(bookList);
        return resData;
    }

    @RequestMapping(value = "/SelectBookNum")
    public ResData selectBookNum(Book book) {
        if (book == null) {
            book = new Book();
        }
        int num = bookService.selectBookNum(book);
        book.setPageSize(num);

        ResData resData = new ResData(1);
        resData.setResult(book);
        return resData;
    }

    @RequestMapping(value = "/SelectBook")
    public ResData selectBook(Book book, String userId) {
        List<Book> bookList = bookService.selectBookList(book);
        book = bookList.get(0);

        if (Base.isString(userId)) {
            History history = new History();
            history.setUserId(userId);
            history.setHistoryId(book.getBookId());
            history.setHistoryTitle(book.getBookTitle());
            history.setHistoryCover(book.getBookCover());
            history.setHistoryCreateTime(Base.getCurrentDate(0, "yyyy-MM-dd HH:mm:ss"));
            history.setCategoryId(1);
            historyService.insertHistory(history);
        }

        ResData resData = new ResData(1);
        resData.setResult(book);
        return resData;
    }

    @RequestMapping(value = "/SelectBookImage")
    public ResData selectBookImage(Book book, HttpServletRequest request, HttpServletResponse response) throws Exception {
//        response.reset();
//        response.setContentType("application/octet-stream");
//        response.setCharacterEncoding("utf-8");
//        response.setHeader("Content-Disposition", "attachment;filename=" + book.getBookFile());
//        response.setHeader("Access-Control-Allow-Origin", "*");
//        response.setContentType("application/pdf");
        ResData resData = new ResData(1);
        String bookId = book.getBookId();
        int pageIndex = book.getPageIndex();
        String bookPath = request.getServletContext().getRealPath("/upload/book");
        File bookPdfFile = new File(bookPath + "/file", book.getBookFile());
        String bookImage = bookId + "_" + pageIndex + ".png";
        File bookImageDir = new File(bookPath + "/image/" + bookId);
        if (!bookImageDir.exists()) {
            bookImageDir.mkdir();
        }
        File bookImageFile = new File(bookImageDir, bookImage);
        int pageSize = PdfUtil.pdfToImage(bookPdfFile, bookImageFile, pageIndex);
        book.setBookPageSize(pageSize);
        bookService.update(book);
        if (pageSize == 0) {
            resData.setCode(0);
            resData.setMsg("书籍不存在");
        }
        book.setBookImage(bookImage);
        resData.setResult(book);
        return resData;
//           InputStream is = PdfUtil.pdfToStream(pdfFile, book.getPageIndex().intValue());
//        if (is != null) {
//            OutputStream os = response.getOutputStream();
//            int read = 0;
//            byte[] buffer = new byte[1024 * 1024];
//            while ((read = is.read(buffer)) != -1) {
//                os.write(buffer, 0, read);
//                os.flush();
//            }
//        }

//        List<Book> bookList = bookService.selectBookList(book);
//        book = bookList.get(0);
//
//        History history = new History();
//        history.setUserId(userId);
//        history.setHistoryId(book.getBookId());
//        history.setHistoryTitle(book.getBookTitle());
//        history.setHistoryCover(book.getBookCover());
//        history.setCategoryId(1);
//        history.setHistoryCreateTime(Base.getCurrentDate(0, "yyyy-MM-dd HH:mm:ss"));
//        historyService.insertHistory(history);

//        ResData resData = new ResData(1);
//        resData.setResult(book);
//        return resData;
    }

}
